using System;
using System.IO;
using System.Xml;
using SharpObjects.Configuration.ConfigurationProviders;

namespace SharpObjects.Configuration.Utils
{
    public class XmlDefinitions
    {
        protected XmlConfigurationProvider definitions;
        protected string resourceVersion = "default";

        public XmlDefinitions(XmlElement definitions, string resourceVersion = "default")
        {
            this.resourceVersion = resourceVersion;
            this.definitions = new XmlConfigurationProvider(definitions, PathMapping.PathMapperFactory.PathMapper);
            this.definitions.ConfigurationVersion = resourceVersion;
        }

        public XmlDefinitions(string xmlDefinitionsFile, string resourceVersion = "default")
        {
            this.resourceVersion = resourceVersion;

            if (xmlDefinitionsFile != null && File.Exists(xmlDefinitionsFile))
            {
                this.definitions = new XmlConfigurationProvider(xmlDefinitionsFile) { ConfigurationVersion = resourceVersion };
            }
            else
            {
                this.definitions = null;
            }
        }



        private string MapTag(string tag, string resourceVersion)
        {
            string path;

            if(resourceVersion == null)
            {
                path = string.Format("//string_resources/string[@resource_id='{0}']/value", tag);
            }
            else
            {
                path = string.Format("//string_resources/string[@resource_id='{0}']/value[@version='{1}']", tag, resourceVersion);
            }

            return path;
        }

        public string this[string tag]
        {
            get
            {
                if (this.definitions != null)
                {
                    string path;

                    path = MapTag(tag, this.resourceVersion);

                    if(this.definitions.HasSetting(path))
                    {
                        return this.definitions.GetString(path);
                    }
                    else
                    {
                        path = MapTag(tag, "default");

                        if(this.definitions.HasSetting(path))
                        {
                            return this.definitions.GetString(path);
                        }
                        else
                        {
                            path = MapTag(tag, null);

                            if(this.definitions.HasSetting(path))
                            {
                                return this.definitions.GetString(path);
                            }
                            else
                            {
                                throw new ArgumentException("No resource found with the ID: " + tag);
                            }
                        }
                    }
                }
                else
                {
                    throw new Exception("No XML definition file was specified");
                }
            }
        }

        //public string this[string tag, string resourceVersion]
        //{
        //    get
        //    {
        //        if (this.definitions != null)
        //        {
        //            string origVersion = this.definitions.ConfigurationVersion;

        //            try
        //            {
        //                this.definitions.ConfigurationVersion = resourceVersion;
        //                string value = this.definitions.GetString(MapTag(tag));
        //                return value;
        //            }
        //            finally
        //            {
        //                this.definitions.ConfigurationVersion = origVersion;
        //            }
        //        }
        //        else
        //        {
        //            throw new Exception("No XML definition file was specified");
        //        }
        //    }
        //}

        //public string this[string tag, string resourceVersion, string defaultValue]
        //{
        //    get
        //    {
        //        if (this.definitions != null)
        //        {
        //            string origVersion = this.definitions.ConfigurationVersion;

        //            try
        //            {
        //                this.definitions.ConfigurationVersion = resourceVersion;
        //                return this.definitions.GetString(MapTag(tag), defaultValue);
        //            }
        //            catch (Exception)
        //            {
        //                return defaultValue;
        //            }
        //            finally
        //            {
        //                this.definitions.ConfigurationVersion = origVersion;    
        //            }
        //        }
        //        else
        //        {
        //            return defaultValue;
        //        }
        //    }
        //}

        //public string this[string tag, string resourceVersion, params object[] args]
        //{
        //    get
        //    {
        //        if (this.definitions != null)
        //        {
        //            string origVersion = this.definitions.ConfigurationVersion;

        //            try
        //            {
        //                this.definitions.ConfigurationVersion = resourceVersion;
        //                string value = this.definitions.GetString(MapTag(tag));
        //                return string.Format(value, args);
        //            }
        //            finally
        //            {
        //                this.definitions.ConfigurationVersion = origVersion;
        //            }
        //        }
        //        else
        //        {
        //            throw new Exception("No XML definition file was specified");
        //        }
        //    }
        //}





    }
}